type FetchParamsType ={
  method?: 'post' | 'get',
  mode?: 'cors'| 'no-cors' | 'same-origin',
  cache?:'default' | 'no-cache' | 'reload' | 'force-cache' | 'only-if-cached',
  credentials?: 'include'| 'same-origin' | 'omit',
  headers?: Object,
  redirect: 'manual' | 'follow' | 'error',
  referrerPolicy?: 'no-referrer'| 'no-referrer-when-downgrade'| 'origin'| 'origin-when-cross-origin'| 'same-origin'| 'strict-origin'| 'strict-origin-when-cross-origin'| 'unsafe-url',
  body?: Object,
}

const defaultoption = { 
  method:'get' ,
  mode:'cors' ,
  cache:'no-cache' ,
  credentials:"same-origin" ,
  headers:{"Content-Type": "application/json"},
  redirect:'follow',
  referrerPolicy:"no-referrer",
  body:{}
}
async function fetch_get(url:string,params={}){
  console.log("get参数params", params);
  console.log("params类型", typeof(params));
  let paramsArray = []

  // 【未解决】
  if(params!={}){
    Object.keys(params).forEach((key)=> paramsArray.push(key + "=" + params[key]))
    if(url.search(/\?/)===-1){
      url += "?" + paramsArray.join("&");
    }else{
      url += "&" + paramsArray.join("&");
    }
  }
  console.log('fetch——get请求地址', url)
  const response = await fetch(url);
  return response.json();
}

async function fetch_post(url,params){
  let opt = {
    method:'post',
    mode:'cors',
    cache:'no-cache',
    credentials:"same-origin",
    headers:{
      "Content-Type": "application/json; charset=UTF-8"
    },
    redirect:'follow',
    referrerPolicy:"no-referrer",
    body: JSON.stringify({})
  }

  Object.keys(params).forEach((item) =>{
    if(item=='body'){
      opt[item] = JSON.stringify(params[item]);
    }else{
      opt[item] = params[item];
    }
  })
  console.log('请求的opt为', opt)
  let res = await fetch(url, opt);
  return res;
}
export {fetch_get, fetch_post};